package com.example.bruger.androidneurosky;

import android.app.AlertDialog;
import android.app.Dialog;
import android.app.DialogFragment;
import android.app.Fragment;
import android.content.Context;
import android.content.DialogInterface;
import android.os.Bundle;
import android.text.Layout;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.EditText;

/**
 * Created by fuke9_000 on 07-04-2015.
 */
public class HighScoreDialog extends DialogFragment {
    private DbHighScoreHelper db;
    private Context parentContext;

    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        final float score = getArguments().getFloat("Score");
        db = new DbHighScoreHelper(parentContext);

        AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
        LayoutInflater inflater = getActivity().getLayoutInflater();
        View view = inflater.inflate(R.layout.activity_highscore_dialog, null);
        final EditText nameField = (EditText) view.findViewById(R.id.high_score_name);
        builder.setView(view)
                .setPositiveButton(R.string.submit_score, new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int id) {
                        HighScore highScore = new HighScore(score, nameField.getText().toString());
                        db.addHighScore(highScore);
                    }
                })
                .setNegativeButton(R.string.cancel_dialog, new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int id) {
                        HighScoreDialog.this.getDialog().cancel();
                    }
                });

        return builder.create();
    }

    public void setContext(Context context) {
        parentContext = context;
    }
}
